package com.retoc.oawx.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.retoc.oawx.common.util.R;
import com.retoc.oawx.config.shiro.JWTUtil;
import com.retoc.oawx.domain.CarUse;
import com.retoc.oawx.domain.Evection;
import com.retoc.oawx.domain.req.EvectionReq;
//import com.retoc.oawx.domain.req.EvectionUpdateReq;
import com.retoc.oawx.domain.req.DeleteReq;
import com.retoc.oawx.domain.req.PageReq;
import com.retoc.oawx.exception.OAException;
import com.retoc.oawx.service.CarUseService;
import com.retoc.oawx.service.EvectionService;
import com.retoc.oawx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/evection")
@Api("出差申请模块")
public class EvectionController {

    @Autowired
    JWTUtil jwtUtil;

    @Autowired
    UserService userService;

    @Autowired
    CarUseService carUseService;

    @Autowired
    EvectionService evectionService;


    @PostMapping("/evectionRequest")
    @ApiOperation("创建出差申请单")
    public R evectionRequest(@RequestBody @Valid EvectionReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        Evection evection=new Evection();
        if (req.getCarId()!=null){
            CarUse carUse=new CarUse();
            Boolean driver = req.getDriver();
            if (driver){
                carUse.setDriver(5);
            }
//        carUse.setDriver(req.getDriver());
            carUse.setCreateTime(DateUtil.date());
            carUse.setCreaterId(userId);
            String carUUID=UUID.randomUUID().toString();
            carUse.setUuid(carUUID);
            DateTime start = DateUtil.parse(req.getStartTime());
            DateTime end = DateUtil.parse(req.getEndTime());
            carUse.setStartTime(start);
            carUse.setEndTime(end);
            carUse.setCarId(req.getCarId());
            carUse.setStatus(0);
            carUse.setReason(req.getPlace()+"出差，需要用车。出差原因："+req.getReason());
            int row = carUseService.createCarUse(carUse);
            if (row!=1){
                throw new OAException("创建用车单失败");
            }
            int carOrder=carUseService.selectCarUseByUuid(carUUID).getId();
            evection.setCarOrder(carOrder);
        }
        evection.setCreateTime(DateUtil.date());
        evection.setCreaterId(userId);
        evection.setUuid(UUID.randomUUID().toString());
        DateTime start = DateUtil.parse(req.getStartTime());
        DateTime end = DateUtil.parse(req.getEndTime());
        evection.setStartTime(start);
        evection.setEndTime(end);
        evection.setStatus(0);
        evection.setReason(req.getReason());
        evection.setPlace(req.getPlace());
        int row = evectionService.createEvection(evection);
        if (row!=1){
            throw new OAException("创建出差单失败");
        }
        return R.ok();
    }
//
    @PostMapping("/evectionUpdate")
    @ApiOperation("更新出差申请单")
    public R useCarUpdate(@RequestBody @Valid EvectionReq req, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        Evection evection=evectionService.selectEvectionById1(req.getId());

        CarUse carUse=new CarUse();
        if (req.getCarId()!=null){
            HashMap hashMap = carUseService.selectCarUseById(evection.getCarOrder());
            Boolean driver = req.getDriver();
            carUse.setId((int)hashMap.get("id"));
            if (driver){
                carUse.setDriver(5);
            }
            carUse.setCarId(req.getCarId());
            carUse.setReason(req.getPlace()+"出差，需要用车。出差原因："+req.getReason());
            carUse.setCreateTime(DateUtil.date());
            carUse.setStatus(0);
            carUse.setStartTime(DateUtil.parseDate(req.getStartTime()));
            carUse.setEndTime(DateUtil.parseDate(req.getEndTime()));
            carUseService.updateCarUse(carUse);
        } else{
            evection.setCarOrder(null);
        }

//        evection.setDriver(req.getDriver());
        evection.setCreateTime(DateUtil.date());
        DateTime start = DateUtil.parse(req.getStartTime());
        DateTime end = DateUtil.parse(req.getEndTime());
        evection.setStartTime(start);
        evection.setEndTime(end);
        evection.setStatus(0);
        evection.setReason(req.getReason());
        int row = evectionService.updateEvection(evection);
        if (row!=1){
            throw new OAException("创建出差单失败，请重新创建");
        }
        return R.ok();
    }
//
    @PostMapping("/searchEvectionByCreater")
    @ApiOperation("查询自己的请假申请")
    public R searchEvectionByCreater(@RequestBody @Valid PageReq req, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        Integer page = req.getPage();
        Integer length = req.getLength();
        Boolean old = req.getOld();
        int start = (page - 1) * length;
        HashMap map = new HashMap<>();
        map.put("createrId", userId);
        map.put("start", start);
        map.put("length", length);
        ArrayList<HashMap> result=new ArrayList<>();
        if (old){
            result=evectionService.selectEvectionListByCreator(map);
        }else {
            result=evectionService.selectOldEvectionListByCreator(map);
        }
        return R.ok().put("result", result);
    }

    @PostMapping("/selectEvectionById")
    @ApiOperation("根据请假单ID查询请假单信息")
    public R selectLeaveById(@Valid @RequestBody DeleteReq req){
        HashMap result = evectionService.selectEvectionById(req.getId());
        return R.ok().put("result", result);
    }

    @DeleteMapping("/deleteEvectionById")
    @ApiOperation("根据用车单ID删除用车单")
    public R deleteEvectionById(@Valid @RequestBody DeleteReq req){
        int i = evectionService.deleteByid(req.getId());
        return R.ok().put("result","已删除"+i+"条记录");
    }
}
